<?php

echo '<html><head></head><body>';
echo '<h1>HI</h1>';

foreach ($_POST as $key => $value) {
    echo 'Debug->$_POST: ' . $key . '->' . $value . '<br />';
}
foreach ($_GET as $key => $value) {
    echo 'Debug->$_GET: ' . $key . '->' . $value . '<br />';
}
echo "doing..";

echo 'assigning $sections..';
$bob = $_GET['section'];
echo 'calling function..';
$arrayFilter = plexGetFilter($bob);

echo "<pre>";
var_dump($arrayFilter);
echo '</pre>';

echo json_encode($arrayFilter);


echo '</body></html>';

function plexGetFilter($section, $category = NULL) {

    echo 'in function..';
    if ($category == NULL) {
        $url = 'http://192.168.0.3:32400/library/sections/' . $section;
        //$logger->info('Getting filters from the Plex library at ' . $url);
    } else {
        echo 'else..';
        $url = 'http://192.168.0.3:32400/library/sections/' . $section . '/' . $category;
        //$logger->info('Searching for categories in the Plex library at ' . $url);
    }

    echo 'xml load from: ' . $url . '..';


    if (!@fopen($url, 'r')) {
        echo 'can\'t read url..';
        return false;
    }

    $xml = @simplexml_load_file($url);

    echo "<pre>";
    var_dump($xml);
    echo '</pre>';

    if (!$xml) {
        echo 'no xml..';
        //$logger->info('No XML found at url: ' . $url);
        echo 'return false..';
        return FALSE;
    }

    if ($xml['viewGroup'] == 'secondary') {
        echo 'foreach..';
        foreach ($xml->Directory as $option) {
            $options[$option['title']] = $option['key'];
        }
    } else {
        //$logger->info('We hit a movie listing at url: ' . $url);
        echo 'return true..';
        return TRUE;
    }
    echo 'return array..';
    return $options;
}

?>
